package com.example.abc.common;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Peter on 2018/4/27.
 */
public class DataTableRequest {
    private int draw;//": 1,
    private int start; // 数据的开始位置，开始于0
    private int length; // 显示数据条数
    private String search; // 搜索条件
    private String orderColumn; // 排序列名
    private String orderDir; // 排序顺序:"asc","desc"
    private List<Column> columns = new ArrayList<>();
    private int columnsNumber = 0;//列的数量

    public DataTableRequest(HttpServletRequest request) {
        this.draw = toInt(request.getParameter("draw"));
        this.start = toInt(request.getParameter("start"));
        this.length = toInt(request.getParameter("length"));
        this.search = request.getParameter("search[value]");
        if(StringUtils.isBlank(search)){
            search = null;
        }
        int orderColunmNumber =  toInt(request.getParameter("order[0][column]"));
        this.orderColumn = getOrderColumn(request,orderColunmNumber);// 此方法会设置columnsNumber。
        if(StringUtils.isBlank(orderColumn)){
            orderColumn = null;
        }
        this.orderDir = request.getParameter("order[0][dir]");
        if(StringUtils.isBlank(orderDir)){
            orderDir = "asc";
        }
        initColumns(request);
    }
    public int getDraw() {
        return draw;
    }

    public int getColumnsNumber() {
        return columnsNumber;
    }

    public void setColumnsNumber(int columnsNumber) {
        this.columnsNumber = columnsNumber;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    private void initColumns(HttpServletRequest request) {

        for(int i=0; i < columnsNumber; i++) {
            String colunm_i = request.getParameter("columns["+ i +"][data]");
            if(StringUtils.isNotBlank(colunm_i)) {
                Column column = new Column(request, i);
                this.columns.add(column);
            }
        }
    }


    public String getSearch() {
        return search;
    }

    private long toLong(String str){
        if(StringUtils.isBlank(str)){
            return 0;
        }else{
            return Long.parseLong(str);
        }
    }
    private int toInt(String str){
        if(StringUtils.isBlank(str)){
            return 0;
        }else{
            return Integer.parseInt(str);
        }
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public String getOrderColumn() {
        return orderColumn;
    }

    public void setOrderColumn(String orderColumn) {
        this.orderColumn = orderColumn;
    }

    public String getOrderDir() {
        return orderDir;
    }

    public void setOrderDir(String orderDir) {
        this.orderDir = orderDir;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    private String getOrderColumn(HttpServletRequest request,int orderColunmNumber) {
        Pattern p = Pattern.compile("columns\\[[0-9]+\\]\\[data\\]");
        @SuppressWarnings("rawtypes")
        Enumeration params = request.getParameterNames();
        List<String> lstOfParams = new ArrayList<String>();
        while(params.hasMoreElements()){
            String paramName = (String)params.nextElement();
            Matcher m = p.matcher(paramName);
            if(m.matches())	{
                lstOfParams.add(paramName);
            }
        }
        String orderColumnParamName =  lstOfParams.get(orderColunmNumber);
        this.columnsNumber = lstOfParams.size();
        return request.getParameter(orderColumnParamName);
    }
}
